{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4f8dc9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3\n",
    "from sagemaker.core.helper.session_helper import get_execution_role, Session\n",
    "from sagemaker.core.transformer import Transformer\n",
    "from sagemaker.serve.model_builder import ModelBuilder\n",
    "from sagemaker.core.image_uris import retrieve\n",
    "\n",
    "region = boto3.Session().region_name\n",
    "\n",
    "role = get_execution_role()\n",
    "print(\"RoleArn: {}\".format(role))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3dcfba2",
   "metadata": {},
   "outputs": [],
   "source": [
    "sagemaker_session = Session()\n",
    "bucket = sagemaker_session.default_bucket()\n",
    "\n",
    "print(\"Demo Bucket: {}\".format(bucket))\n",
    "prefix = \"demo-transform\"\n",
    "reports_prefix = \"{}/reports\".format(prefix)\n",
    "s3_report_path = \"s3://{}/{}\".format(bucket, reports_prefix)\n",
    "\n",
    "transform_output_path = \"s3://{}/{}/transform-outputs\".format(bucket, prefix)\n",
    "\n",
    "print(\"Transform Output path: {}\".format(transform_output_path))\n",
    "print(\"Report path: {}\".format(s3_report_path))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c371a031",
   "metadata": {},
   "source": [
    "### Deploy Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e82528a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_file_name = \"xgb-churn-prediction-model.tar.gz\"\n",
    "\n",
    "!aws s3 cp data/{model_file_name} s3://{bucket}/{prefix}/{model_file_name}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8534cee8",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_url = \"https://{}.s3-{}.amazonaws.com/{}/{}\".format(bucket, region, prefix, model_file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c15e08c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_uri = retrieve(\"xgboost\", boto3.Session().region_name, \"0.90-1\")\n",
    "\n",
    "model_builder = ModelBuilder(\n",
    "    image_uri=image_uri,\n",
    "    s3_model_data_url=model_url,\n",
    "    role_arn=role,\n",
    "    sagemaker_session=sagemaker_session,\n",
    ")\n",
    "\n",
    "model_builder.build(model_name=\"my-transform-model\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5aff7171",
   "metadata": {},
   "source": [
    "### Validation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26f26eb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dataset used to get predictions\n",
    "\n",
    "!aws s3 cp data/validation.csv s3://{bucket}/{prefix}/transform_input/validation/validation.csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70cb6a17",
   "metadata": {},
   "outputs": [],
   "source": [
    "transformer = Transformer(\n",
    "    model_name=\"my-transform-model\",\n",
    "    instance_count=1,\n",
    "    instance_type=\"ml.m5.xlarge\",\n",
    "    accept=\"text/csv\",\n",
    "    assemble_with=\"Line\",\n",
    "    output_path=transform_output_path,\n",
    "    sagemaker_session=sagemaker_session,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a5bb3c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_input=f\"s3://{bucket}/{prefix}/transform_input/validation\"\n",
    "\n",
    "transform_arg = transformer.transform(\n",
    "    data_input,\n",
    "    content_type=\"text/csv\",\n",
    "    split_type=\"Line\",\n",
    "    # exclude the ground truth (first column) from the validation set\n",
    "    # when doing inference.\n",
    "    input_filter=\"$[1:]\",\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
